<template>
  <div>
    <div class="options">
      <el-button @click="info.isShow=true" type="primary" round plain>新增</el-button>
    </div>
    <el-table :data="roleList" style="width: 100%">
      <el-table-column prop="id" label="序号" min-width="80"> </el-table-column>
      <el-table-column prop="rolename" label="角色名称" min-width="180">
      </el-table-column>
      <el-table-column prop="status" label="角色状态" min-width="180">
        <template v-slot="props">
          <el-button type="success" size="mini" round plain v-if="props.row.status === 1"
            >正常</el-button
          >
          <el-button type="danger" size="mini" round plain v-else>禁用</el-button>
        </template>
      </el-table-column>
      <el-table-column label="操作">
       <template v-slot="$props">
          <el-button @click="edit($props.row)" type="success" round size="mini" plain>编辑</el-button>
          <el-button @click="del($props.row.id)" type="danger" round size="mini" plain>删除</el-button>
       </template>
      </el-table-column>
    </el-table>
    <!-- 调用对话框组件 -->
    <Dialog ref="dialog" :info="info"/>
  </div>
</template>

<script>
// 导入对话框组件
import Dialog from './Dialog'
// 导入vue辅助函数
import { mapState, mapActions,mapMutations } from "vuex";
export default {
  components:{Dialog},
  data(){
    return {
      info:{
        // 控制对话框组件显示状态
        isShow:false
      }
    }
  },
  computed: {
    ...mapState("role", ["roleList"]),
  },
  methods: {
    ...mapActions("role", ["getRoleListAction"]),
    ...mapMutations('role',['roleDel']),
    edit(row){
      // 1-显示对话框组件
      this.info.isShow=true;
      // 2-将待编辑的角色信息传递给对话框组件
      this.$refs.dialog.setFormData(row);
    },
    // 删除角色
    del(id){
      this.$myConfirm(()=>{
        this.$axios.post('/roledelete',{id}).then(res=>{
          if(res.code===200){
            // 移除页面中被删除的角色
            this.roleDel(id);
          }
        });
      })
    }
  },
  created() {
    // 请求角色列表
    this.getRoleListAction();
  },
};
</script>

<style>
</style>